home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 301-325 / disk_313 / uucp / uucp1.lzh / src / GUtil / gnote.c < prev    next >
C/C++ Source or Header  |  1990-01-10  |  3KB  |  198 lines

  1.  
  2. /*
  3.  *  GNOTE.C
  4.  *
  5.  *  (C) Copyright 1989-1990 by Matthew Dillon,  All Rights Reserved.
  6.  *
  7.  *  GNOTE hellofile notefile
  8.  *
  9.  *  Allow the user to enter a note and append it to notefile.  See
  10.  *  Getty. Password entry should have a '*' in front of the executable
  11.  *  name for Getty to provide automatic stdin/stdout
  12.  *
  13.  */
  14.  
  15. #include <proto/all.h>
  16. #include <stdio.h>
  17. #include <time.h>
  18. #include "/version.h"
  19.  
  20. IDENT(".00");
  21.  
  22. char *GetLine();
  23. void PutDaChar();
  24. void FlushChars();
  25.  
  26. void
  27. main(ac, av)
  28. char *av[];
  29. {
  30.     time_t t;
  31.     char *ptr;
  32.     FILE *fi;
  33.  
  34.     time(&t);
  35.  
  36.     if (ac < 3) {
  37.     printf("gnote: expect 2 arguments, got %d\n", ac - 1);
  38.     fflush(stdout);
  39.     Delay(50*2);
  40.     exit(1);
  41.     }
  42.     printf("%s %s", Ident, ctime(&t));
  43.     if (fi = fopen(av[1], "r")) {
  44.     char buf[128];
  45.     while (fgets(buf, sizeof(buf), fi))
  46.         fputs(buf, stdout);
  47.     fclose(fi);
  48.     }
  49.     fflush(stdout);
  50.  
  51.     if ((fi = fopen(av[2], "a")) == NULL) {
  52.     puts("couldn't open note file");
  53.     fflush(stdout);
  54.     Delay(50*2);
  55.     exit(1);
  56.     }
  57.     fprintf(fi, "**NOTE** %s", ctime(&t));
  58.  
  59.     while (ptr = GetLine()) {
  60.     if (strcmp(ptr, ".") == 0)
  61.         break;
  62.     if (strncmp(ptr, "**NOTE**", 8) == 0)
  63.         fprintf(fi, " ");
  64.     fprintf(fi, "%s\n", ptr);
  65.     }
  66.     puts("Thank you, hanging up in 5 seconds");
  67.     fflush(stdout);
  68.     fclose(fi);
  69.     Delay(50*5);
  70. }
  71.  
  72.  
  73. char *
  74. GetLine()
  75. {
  76.     static char buf[256];
  77.     static char tab[256];
  78.     short col = 0;
  79.     short i = 0;
  80.     short limit = 5*60;     /*    5 minute idle timeout    */
  81.     short lcnt = 0;
  82.     short c;
  83.  
  84.     for (;;) {
  85.     c = GetDaChar();
  86.     if (c == EOF) {
  87.         clrerr(stdin);
  88.         ++lcnt;
  89.         if (lcnt == limit - 30) {
  90.         puts("\r\n(idle, hangup in 30)\r\n");
  91.         fflush(stdout);
  92.         }
  93.         if (lcnt == limit)
  94.         break;
  95.         continue;
  96.     }
  97.     lcnt = 0;
  98.  
  99.     switch(c) {
  100.     case 10:
  101.     case 13:
  102.         PutDaChar('\r');
  103.         PutDaChar('\n');
  104.         FlushChars();
  105.         buf[i] = 0;
  106.         return(buf);
  107.     case 8:
  108.     case 127:
  109.         if (i) {
  110.         short n = 1;
  111.         --i;
  112.         if (buf[i] == 9)
  113.             n = tab[i];
  114.         col -= n;
  115.         while (n--) {
  116.             PutDaChar('\010');
  117.             PutDaChar(' ');
  118.             PutDaChar('\010');
  119.         }
  120.         }
  121.         break;
  122.     case 'x'&0x1F:
  123.         PutDaChar('^');
  124.         PutDaChar('X');
  125.         PutDaChar('\r');
  126.         PutDaChar('\n');
  127.         i = 0;
  128.         break;
  129.     case 'r'&0x1F:
  130.         PutDaChar('\r');
  131.         PutDaChar('\n');
  132.         FlushChars();
  133.         buf[i] = 0;
  134.         printf("%s", buf);
  135.         fflush(stdout);
  136.         break;
  137.     case 9:
  138.         if (i < sizeof(buf) - 2) {
  139.         tab[i] = 8 - (col & 7);
  140.         col += tab[i] - 1;  /*    because we ++ below */
  141.         }
  142.         /* fall through */
  143.     default:
  144.         if (i < sizeof(buf) - 2) {
  145.         PutDaChar(c);
  146.         buf[i++] = c;
  147.         ++col;
  148.         } else {
  149.         PutDaChar('g' & 0x1F);
  150.         }
  151.         break;
  152.     }
  153.     }
  154.     return(NULL);
  155. }
  156.  
  157. void
  158. FlushChars()
  159. {
  160.     PutDaChar(256);
  161. }
  162.  
  163. void
  164. PutDaChar(c)
  165. {
  166.     static char buf[256];
  167.     static short len;
  168.  
  169.     if (c == 256 || len == sizeof(buf)) {
  170.     if (len)
  171.         write(0, buf, len);
  172.     len = 0;
  173.     }
  174.     if (c != 256) {
  175.     buf[len++] = c;
  176.     }
  177. }
  178.  
  179.  
  180. GetDaChar()
  181. {
  182.     static char buf[256];
  183.     static short idx, len;
  184.  
  185.     if (idx == len) {
  186.     if (read(0, buf, 0) < 0)
  187.         FlushChars();
  188.     idx = 0;
  189.     len = read(0, buf, sizeof(buf));
  190.     if (len <= 0) {
  191.         len = 0;
  192.         return(EOF);
  193.     }
  194.     }
  195.     return((int)buf[idx++]);
  196. }
  197.  
  198.